<?php
namespace app\center\logic;

class SourceLogic extends BaseLogic {

    /**
     * 更新抄送为已读
     * @param $copyid
     * @param $menu_type
     * @return mixed
     */
    public function save_copy_read($copyid,$menu_type){
        if($menu_type == 4){
            $maps = [];
            $maps[] = ['id','=',$copyid];

            $c_data = [];
            $c_data['is_read'] = 1;
            $r = model('copy')->force()->save($c_data,$maps);
            if($r){
                return $this->get_response(1,'OK');
            }
            return $this->get_response(0,'失败');
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 获取批复
     * @param $openid
     * @param $sourceid
     * @param $source
     * @param $menu_type
     * @return mixed
     */
    public function get_reply($openid,$sourceid,$source,$menu_type){
        $list = [];
        if($menu_type == 5){
            $maps = [];
            $maps[] = ['from_openid','=',$openid];
            $maps[] = ['sourceid','=',$sourceid];
            $maps[] = ['source','=',$source];
            $maps[] = ['explain','<>',''];
            $maps[] = ['status','=',1];
            $list = model('copy')->get_list($maps,'id,explain,copy_openid,reply_time','reply_time desc');
            foreach($list as $k => $v){
                //查找用户
                $u_maps = [];
                $u_maps[] = ['openid','=',$v['copy_openid']];
                $u_detail = model('user')->get_detail($u_maps,'id,username');

                $list[$k]['username'] = $u_detail['username'];
            }
        }
        $data['list'] = $list;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 更新批复为已读
     * @param $openid
     * @param $sourceid
     * @param $source
     * @param $menu_type
     * @return mixed
     */
    public function save_reply_read($openid,$sourceid,$source,$menu_type){
        if($menu_type == 5){
            $maps = [];
            $maps[] = ['from_openid','=',$openid];
            $maps[] = ['sourceid','=',$sourceid];
            $maps[] = ['source','=',$source];
            $maps[] = ['explain','<>',''];
            $maps[] = ['status','=',1];

            $c_data = [];
            $c_data['is_reply_read'] = 1;
            $r = model('copy')->force()->save($c_data,$maps);
            if($r){
                return $this->get_response(1,'OK');
            }
            return $this->get_response(0,'失败');
        }
        return $this->get_response(1,'OK');
    }
}